package com.AES;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import javax.swing.JPasswordField;

public class windows
{

    private JFrame frmEngry;
    private JFileChooser ch;
    private File file;
    private String key=null;
    private JPasswordField passwordField;
    private FileInputStream fis;
    private FileOutputStream fos;

    public windows()
    {
	frmEngry=new JFrame();
	frmEngry.setTitle("Encrypt");
	frmEngry.setBounds(100,100,164,198);
	frmEngry.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	frmEngry.getContentPane().setLayout(null);
	frmEngry.setResizable(false);
	frmEngry.setLocationRelativeTo(null);
	JButton button=new JButton("選擇檔案");
	JButton button_2=new JButton("加密");
	JButton button_1=new JButton("解密");
	button.setBounds(14,13,130,29);
	button_2.setBounds(14,55,130,29);
	button_1.setBounds(14,97,130,29);
	passwordField=new JPasswordField();
	passwordField.setBounds(14,139,130,14);
	frmEngry.getContentPane().add(passwordField);
	frmEngry.getContentPane().add(button);
	frmEngry.getContentPane().add(button_2);
	frmEngry.getContentPane().add(button_1);
	frmEngry.setVisible(true);
	button.addActionListener(new ActionListener()
	{
	    public void actionPerformed(ActionEvent arg0)
	    {
		ch=new JFileChooser();
		ch.showOpenDialog(null);
		file=ch.getSelectedFile();
	    }
	});
	button_1.addActionListener(new ActionListener()
	{
	    @SuppressWarnings("deprecation")
	    public void actionPerformed(ActionEvent arg0)
	    {
		try
		{

		    key=passwordField.getText();
		    if(key.length()>=8)
		    {
			fis=new FileInputStream(file.getAbsoluteFile());
			fos=new FileOutputStream("D:\\dencrypted");
			new Thread(new EncodeFile("d",key,fis,fos,file)).start();
		    }
		    else
		    {
			JOptionPane.showMessageDialog(null,"At least 8 char in password");
		    }
		}
		catch (Throwable e)
		{
		    e.printStackTrace();
		}
	    }
	});
	button_2.addActionListener(new ActionListener()
	{
	    @SuppressWarnings("deprecation")
	    public void actionPerformed(ActionEvent arg0)
	    {
		try
		{
		    key=passwordField.getText();
		    if(key.length()>=8)
		    {
			fis=new FileInputStream(file.getAbsoluteFile());
			fos=new FileOutputStream("D:\\encrypted");
			new Thread(new EncodeFile("e",key,fis,fos,file)).start();
		    }
		    else
		    {
			JOptionPane.showMessageDialog(null,"At least 8 char in password");
		    }
		}
		catch (Throwable e)
		{
		    e.printStackTrace();
		}
	    }
	});
    }

    public static void main(String[] args)
    {
	new windows();
    }

}
